from django.db import models

from goods.models import Good
from users.models import User

# Create your models here.
# 优惠券类型
class CouponType(models.Model):
    type_name = models.CharField('优惠券类型',max_length=30)

    def __str__(self):
        return self.type_name

    class Meta:
        db_table = 'coupon_type'
        verbose_name_plural = '优惠券类型'


# 优惠券
class Coupon(models.Model):
    name = models.CharField('优惠券名称',max_length=100)
    brief = models.TextField('优惠券介绍',default='')
    coupon_type = models.ForeignKey(CouponType,on_delete=models.CASCADE,null=True,blank=True,verbose_name='优惠券类型')
    good = models.ForeignKey(Good,on_delete=models.CASCADE,null=True,blank=True,verbose_name='单品消费')
    coupon_num = models.IntegerField('数量',default=0)
    start_time = models.DateTimeField('开始使用时间')
    over_time = models.DateTimeField('结束使用时间')
    start_get_time = models.DateTimeField('领劵开始时间')
    end_get_time = models.DateTimeField('领劵结束时间')
    remark = models.CharField('备注',max_length=300)

    def __str__(self):
        return self.name

    class Meta:
        db_table = 'coupon'
        verbose_name_plural = '优惠券'


# 优惠券 --类型 --具体的值
class CouponValue(models.Model):
    coupon = models.ForeignKey(Coupon,on_delete=models.CASCADE,verbose_name='优惠券')
    coupon_type = models.ForeignKey(CouponType,on_delete=models.CASCADE,verbose_name='优惠券类型')
    value = models.IntegerField('类型的值',default=0)

    def __str__(self):
        return '%s:%s:%s'%(self.coupon.name,self.coupon_type.type_name,self.value)

    class Meta:
        db_table = 'coupon_value'
        verbose_name_plural = '优惠券的值'


# 用户领取优惠卷
class UserGetCoupon(models.Model):
    COUPON_STATUS = (
        (0,'未使用'),
        (1,'已使用'),
        (2,'已过期'),
    )

    user = models.ForeignKey(User,on_delete=models.CASCADE,verbose_name='用户')
    coupon = models.ForeignKey(Coupon,on_delete=models.CASCADE,verbose_name='优惠券')
    status = models.SmallIntegerField('卷的状态',default=0,choices=COUPON_STATUS)
    get_time = models.DateTimeField('领取时间',auto_now_add=True)
    use_time = models.DateTimeField('使用时间',null=True,blank=True)

    def __str__(self):
        return "%s:%s-%s"%(self.user.username,self.coupon.name,self.get_status_display())

    class Meta:
        db_table = 'user_get_coupon'
        verbose_name_plural = '用户领劵'




